;
(function() {
	module.exports = {
		prefix: "/alipay",
		get: {
			"/cacert.pem": function() {
				this.response.sendFile("./cacert.pem", {
					root: "./"
				});
			},
			//即时到帐的回调接口
			"/create_direct_pay_by_user/return_url": function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				console.log("create_direct_pay_by_user/return_url", this.request.query);
				var self = this;
				var result = alipay_instance.create_direct_pay_by_user_return(this.request, function(result) {
					var pay_ach_id = self.request.query.out_trade_no;
					var pay_ach = PayAchCon.get(pay_ach_id);
					if (pay_ach) {
						var ach = pay_ach._getAch();
						var bus_id = ach.info.bus_id;
						var bus = BusCon.get(bus_id);
						var url = bus.getOneVisitableUrl();
						self.body = ResponObj("refresh", url + "/pay-beta.html#?ach_id=" + ach._id);
					} else {
						self.body = ResponObj("refresh", "http://www.dotnar.com");
					}
				}, self);
			},
			"/trade_create_by_buyer/return_url": function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				console.log("trade_create_by_buyer/return_url", this.request.query);
				var self = this;
				var result = alipay_instance.trade_create_by_buyer_return(this.request, function(result) {
					self.body = result;
				}, self);
			},
			"/create_partner_trade_by_buyer/return_url": function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				console.log("create_partner_trade_by_buyer/return_url", this.request.query);
				var self = this;
				var result = alipay_instance.create_partner_trade_by_buyer_return(this.request, function(result) {
					self.body = result;
				}, self);
			},
			//网页支付回调接口
			"/wap_trade_create_direct/return_url": function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				console.log("wap_trade_create_direct/return_url", this.request.query);
				var self = this;
				var result = alipay_instance.wap_trade_create_direct_return(this.request, function(result) {
					self.body = result;
				}, self);
			}
		},
		post: {
			//即时到帐
			'/create_direct_pay_by_user': function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				var body = this.request.body;
				var ach_id = body.WIDout_trade_no;
				var ach = AchCon.get(ach_id);
				if (!ach) {
					throwCusError("订单号有误，找不到指定订单数据")
				}
				var user = role.getUser(this);
				var pay_ach_id = user.callBackToPayAch(ach_id);
				var data = {
					out_trade_no: pay_ach_id,
					subject: body.WIDsubject,
					total_fee: ach.finally_cash, //body.WIDtotal_fee,
					body: body.WIDbody,
					show_url: body.WIDshow_url
				};
				var result = alipay_instance.create_direct_pay_by_user(data)
				this.body = ResponObj("html", result);
			},
			"/create_direct_pay_by_user_form_ach_id/:ach_id": function() {
				var ach_id = this.params.ach_id;
				var body = this.request.body;
				var user = role.getUser(this);
				var bus = role.getBusInfo(this);
				var ach = AchCon.get(ach_id);
				if (!ach) {
					throwCusError("订单号有误，找不到指定订单数据")
				}
				var pay_ach_id = user.callBackToPayAch(ach_id);
				var data = {
					out_trade_no: pay_ach_id,
					subject: body.WIDsubject || ("来自“" + ach.info.bus_id + "”的订单"),
					total_fee: ach.finally_cash,
					body: body.WIDbody || "",
					show_url: body.WIDshow_url || ""
				};
				var alipay_instance = new AlipayInstance(bus);

				var result = alipay_instance.create_direct_pay_by_user(data, body.type);

				this.body = ResponObj(body.type || "html", result);
			},
			"/mobile_securitypay_pay_form_ach_id/:ach_id": function() {
				var ach_id = this.params.ach_id;
				var body = this.request.body;
				var user = role.getUser(this);
				var bus = role.getBusInfo(this);
				var ach = AchCon.get(ach_id);
				if (!ach) {
					throwCusError("订单号有误，找不到指定订单数据")
				}
				var pay_ach_id = user.callBackToPayAch(ach_id);
				var data = {
					out_trade_no: pay_ach_id,
					subject: body.WIDsubject || ("来自“" + ach.info.bus_id + "”的订单"),
					total_fee: ach.finally_cash,
					body: body.WIDbody || "",
					show_url: body.WIDshow_url || ""
				};
				var alipay_instance = new AlipayInstance(bus);

				var result = alipay_instance.mobile_securitypay_pay(data, body.type);

				this.body = ResponObj(body.type || "string", result);
			},
			//即时到账批量退款有密接口
			"refund_fastpay_by_platform_pwd": function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				var body = this.request.body;
				var data = {
					refund_date: body.WIDrefund_date,
					batch_no: body.WIDbatch_no,
					batch_num: body.WIDbatch_num,
					detail_data: body.WIDdetail_data
				};
				var result = alipay_instance.refund_fastpay_by_platform_pwd(data)
				this.body = ResponObj("html", result);
			},
			//担保交易
			"/create_partner_trade_by_buyer": function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				var body = this.request.body;
				var ach_id = body.WIDout_trade_no;
				var ach = AchCon.get(ach_id);
				if (!ach) {
					throwCusError("订单号有误，找不到指定订单数据")
				}
				var user = role.getUser(this);
				var pay_ach_id = user.callBackToPayAch(ach_id);
				var data = {
					out_trade_no: pay_ach_id,
					subject: body.WIDsubject,
					price: ach.finally_cash, //body.WIDprice,
					quantity: body.WIDquantity,
					logistics_fee: body.WIDlogistics_fee,
					logistics_type: body.WIDlogistics_type,
					logistics_payment: body.WIDlogistics_payment,
					body: body.WIDbody,
					show_url: body.WIDshow_url,
					receive_name: body.WIDreceive_name,
					receive_address: body.WIDreceive_address,
					receive_zip: body.WIDreceive_zip,
					receive_phone: body.WIDreceive_phone,
					receive_mobile: body.WIDreceive_mobile
				};

				var result = alipay_instance.create_partner_trade_by_buyer(data)
				this.body = ResponObj("html", result);
			},
			//标准双接口
			"/trade_create_by_buyer": function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				var body = this.request.body;

				//构造要请求的参数数组，无需改动
				var data = {
					out_trade_no: body.WIDout_trade_no,
					subject: body.WIDsubject,
					price: body.WIDprice,
					quantity: body.WIDquantity,
					logistics_fee: body.WIDlogistics_fee,
					logistics_type: body.WIDlogistics_type,
					logistics_payment: body.WIDlogistics_payment,
					body: body.WIDbody,
					show_url: body.WIDshow_url,
					receive_name: body.WIDreceive_name,
					receive_address: body.WIDreceive_address,
					receive_zip: body.WIDreceive_zip,
					receive_phone: body.WIDreceive_phone,
					receive_mobile: body.WIDreceive_mobile
				};

				var result = alipay_instance.trade_create_by_buyer(data, res);

				this.body = ResponObj("html", result);
			},
			//网页支付
			"/wap_trade_create_direct": function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				var body = this.request.body;
				var ach_id = body.WIDout_trade_no;
				var ach = AchCon.get(ach_id);
				if (!ach) {
					throwCusError("订单号有误，找不到指定订单数据")
				}
				var user = role.getUser(this);
				var pay_ach_id = user.callBackToPayAch(ach_id);
				var data = {
					out_trade_no: pay_ach_id,
					subject: body.WIDsubject,
					total_fee: ach.finally_cash, //body.WIDtotal_fee,
					body: body.WIDbody,
					show_url: body.WIDshow_url
				};
				var result = alipay_instance.wap_trade_create_direct(data)
				this.body = ResponObj("html", result);
			},
			"/wap_trade_create_direct_form_ach_id/:ach_id": function() {
				var ach_id = this.params.ach_id;
				var body = this.request.body;
				var user = role.getUser(this);
				var bus = role.getBusInfo(this);
				var ach = AchCon.get(ach_id);
				if (!ach) {
					throwCusError("订单号有误，找不到指定订单数据")
				}
				var pay_ach_id = user.callBackToPayAch(ach_id);
				var data = {
					out_trade_no: pay_ach_id,
					subject: body.WIDsubject || ("来自“" + ach.info.bus_id + "”的订单"),
					total_fee: ach.finally_cash,
					body: body.WIDbody || "",
					show_url: body.WIDshow_url || ""
				};
				var alipay_instance = new AlipayInstance(bus);

				var result = alipay_instance.wap_trade_create_direct(data, body.type);

				this.body = ResponObj(body.type || "html", result);
			},
			//即时到帐的通知接口
			"/create_direct_pay_by_user/notify_url": function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				console.log("create_direct_pay_by_user/notify_url", this.request.query, this.request.body);
				var self = this;
				alipay_instance.create_direct_pay_by_user_notify(this.request, function(result) {
					self.body = result;
				}, self)
			},
			//内部测试接口
			"/create_direct_pay_by_user/notify_url/succ": function() {
				var body = this.request.body;
				var ach_id = body.WIDout_trade_no;
				var user = role.getUser(this);
				var pay_ach_id = user.callBackToPayAch(ach_id);
				var PayAchCon = require("../../core/pay_ach/_con");
				var pay_ach = PayAchCon.get(pay_ach_id);
				if (!pay_ach) {
					throwCusError("找不到指定支付回调信")
				} else {
					pay_ach.endPayAch();
					this.body = "支付成功"
				}
			},
			// 即时到帐的退款通知接口
			"/refund_fastpay_by_platform_pwd/notify_url": function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				console.log("refund_fastpay_by_platform_pwd/notify_url", this.request.query, this.request.body);
				var self = this;
				alipay_instance.refund_fastpay_by_platform_pwd_notify(this.request, function(result) {
					self.body = result;
				}, self);
			},
			//担保交易的通知接口
			"/create_partner_trade_by_buyer/notify_url": function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				console.log("create_partner_trade_by_buyer/notify_url", this.request.query, this.request.body);
				var self = this;
				alipay_instance.create_partner_trade_by_buyer_notify(this.request, function(result) {
					console.log("result: create_partner_trade_by_buyer/notify_url", result);
					self.body = result;
				});
			},
			//标准双接口的通知接口
			"/trade_create_by_buyer/notify_url": function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				console.log("trade_create_by_buyer/notify_url", this.request.query, this.request.body);
				var self = this;
				alipay_instance.trade_create_by_buyer_notify(this.request, function(result) {
					self.body = result;
				}, self);
			},
			//网页支付通知接口
			"/wap_trade_create_direct/notify_url": function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				console.log("wap_trade_create_direct/notify_url", this.request.query, this.request.body);
				var self = this;
				alipay_instance.wap_trade_create_direct_notify(this.request, function(result) {
					self.body = result;
				}, self)
			},
			//App支付通知接口
			"/mobile_securitypay_pay/notify_url": function() {
				var bus = role.getBusInfo(this);
				var alipay_instance = new AlipayInstance(bus);
				console.log("mobile_securitypay_pay/notify_url", this.request.query, this.request.body);
				var self = this;
				alipay_instance.mobile_securitypay_pay_notify(this.request, function(result) {
					self.body = result;
				}, self)
			}
		}
	};
	var AlipayInstance = require("./alipay_instance");
	var role = require("./role");
	var ResponObj = require("../_include/responObj");
	var PayAchCon = require("../../core/pay_ach/_con");
	var AchCon = require("../../core/ach/_con");
	var BusCon = require("../../core/bus/_con");
}());